<?php
JHTML::_('behavior.modal');
JHTML::_('behavior.tooltip');

$currentScheme = 'left-content-right';

if (!$this->countModules('position-7')) $currentScheme = str_replace('left','',$currentScheme);
if (!$this->countModules('rightload')) $currentScheme = str_replace('right','',$currentScheme);

$schemeOutput = '';
$currentScheme = explode("-",$currentScheme);

if (is_array ($currentScheme)) {
   foreach ($currentScheme as $item) {
   	   if ($item) $schemeOutput[] = $item;
   }
}

$currentScheme = "scheme_".count($schemeOutput);

jimport( 'joomla.application.module.helper' );
class DJModuleHelper extends JModuleHelper {
	function renderModules($position, $chrome = 'none', $modulesPerRow = 1) {
		if (!$position) return false;
		$html = '';
		if ($modules = parent::getModules( $position )) {
			$attribs['style'] = $chrome;
			$html .= '<div class="'.$position.' count_'.count($modules).'">';
			for ($i = 0, $k = 0; $i < count($modules); $i++) {
				$className = $position.'-in';
				if ($i % $modulesPerRow == 0) {
					$className .= ' '.$position.'-row-first';
					$rowClass = 'rowcount_';
					$scheme = $modulesPerRow;
					if ($i + $modulesPerRow > count($modules)) {
						$scheme = count($modules) % $modulesPerRow;
					}
					$html .= '<div class="'.$rowClass.$scheme.' clearfix">';
				}
				if (($i+1) % $modulesPerRow == 0 || $i == count($modules)-1) {
					$className .= ' '.$position.'-row-last';
				}
				$html .= '<div class="'.$className.'">';
				$html .= '<div class="'.$position.'-bg'.'">';
				$html .= parent::renderModule($modules[$i], $attribs);
				$html .= '</div>';
				$html .= '</div>';
				if (($i+1) % $modulesPerRow == 0 || $i == count($modules)-1) {
					$html .= '</div>';
				}
				$k = 1 - $k;
			}
			$html .= '</div>';
		}
		return $html;
	}
}

?>